package com.scud.product.service.impl;

import com.scud.client.system.ISystemClient;
import com.scud.common.error.ErrorConstants;
import com.scud.common.exception.ScudException;
import com.scud.common.utils.SecurityUtil;
import com.scud.model.PageDto;
import com.scud.model.PageResult;
import com.scud.model.product.dto.AuditPageDto;
import com.scud.model.product.dto.AuditStatusDto;
import com.scud.model.product.pojo.AuditLog;
import com.scud.model.product.pojo.Product;
import com.scud.product.mapper.AuditLogMapper;
import com.scud.product.mapper.ProductMapper;
import com.scud.product.service.AuditService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author vermouth
 */

@Service
public class AuditServiceImpl implements AuditService {

    @Autowired
    private ISystemClient systemClient;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private AuditLogMapper auditLogMapper;

    /**
     * 获取当前审核人员的商品审核列表
     * @param :
     * @return Result
     * @author vermouth
     */
    @Override
    public PageResult<Product> getAuditList(AuditPageDto dto) {
        String token = (String) SecurityContextHolder.getContext().getAuthentication().getCredentials();
        //获取所有任何人员的id集合
        List<Long> auditIds = systemClient.getAuditIds(token);
        //获取当前任何人员的id
        Long userId = SecurityUtil.getUser().getId();
        //获取当前审核人员在所有审核人员中的索引和总数
        int index=-1;
        int auditNum=auditIds.size();
        for (int i = 0; i < auditIds.size(); i++) {
            if (auditIds.get(i).equals(userId)){
                index=i;
                break;
            }
        }
        if (index==-1){
            throw new ScudException(ErrorConstants.ROLE_ERROR);
        }
        //查询符合要求的待审核商品
        dto.setIndex(index);
        dto.setAuditCount(auditNum);
        dto.setPage((dto.getPage()-1)*dto.getPageSize());
        List<Product> productList=productMapper.selectByAuditQuery(dto);
        Long total=productMapper.countAudit(dto);
        //封装返回对象
        PageResult<Product> pageResult = new PageResult<>();
        pageResult.setTotal(total);
        pageResult.setList(productList);
        return pageResult;
    }

    /**
     * 审核人员修改审核状态
     * @param dto:
     * @return Result
     * @author vermouth
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void auditStatus(AuditStatusDto dto) {
        //判断审核状态，是否违法
        if (!dto.getStatus().equals((short)0) && !dto.getStatus().equals((short)1)){
            throw new ScudException(ErrorConstants.STATUS_ERROR);
        }
        //根据审核状态的不同，判断审核信息是否为空
        if (dto.getStatus().equals((short)0) && (dto.getInfo()==null || dto.getInfo().equals(""))){
            throw new ScudException(ErrorConstants.ERROR_INFO_NOT_EXISTS);
        }
        //修改商品表中的审核状态
        productMapper.updateStatus(dto.getProductId(), dto.getStatus());
        //获取当前用户id
        Long userId = SecurityUtil.getUser().getId();
        //将审核信息添加到审核日志表当中
        AuditLog auditLog = new AuditLog();
        BeanUtils.copyProperties(dto,auditLog);
        auditLog.setUserId(userId);
        auditLog.setCreatTime(LocalDateTime.now());
        auditLogMapper.insert(auditLog);
    }


}
